package com.wx.service.impl;

import com.wx.config.UserConfig;
import com.wx.entity.News;
import com.wx.entity.PicMessage;
import com.wx.entity.TextMessage;
import com.wx.service.WxCheckSignatureService;
import com.wx.utils.MessageUtil;
import org.apache.maven.shared.utils.logging.MessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author DKH
 * @version 1.0.0
 * @createTime 2022年01月27日 14:03:00
 */
@Service
public class WxCheckSignatureServiceImpl implements WxCheckSignatureService {

    public static final ConcurrentHashMap<String,String> concurrentHashMap = new ConcurrentHashMap<>();

    @Autowired
    private MessageUtil messageUtil;

    @Autowired
    private UserConfig userConfig;

    @Override
    public String messageHandle(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String> map = messageUtil.parseXml(request);

        String msgType = map.get("MsgType");
        // 用户提交的内容
        String content = map.get("Content");

        // 格式为------->修改情话: 修改的内容
        if (content!= null && content.contains("修改情话")) {
            String[] split = content.split(":");
            messageUtil.setText(split[1]);
        }

        // 格式为------->修改地区: 修改的内容
        if (content!= null && content.contains("修改地区")) {
            String[] split = content.split(":");
            userConfig.setCity(split[1]);
        }



        /**
         * 保存用户的id和公众号id，方便定时器发送
         */
        if (!concurrentHashMap.containsValue(map.get("FromUserName"))){
            concurrentHashMap.put(UUID.randomUUID().toString(),map.get("FromUserName"));
        }

        return autoResp(msgType,map);
    }

    /**
     * 根据消息类型自动回复
     */
    private String autoResp(String type,Map<String, String> map){

        switch (type){
            // 文本类型
            case MessageUtil.RESP_MESSAGE_TYPE_TEXT : {
                News news = new News();
                news.setTitle("亲爱的您，欢迎来访");
                //文本描述
                news.setDescription("早安晚安定时器已经开始运行");

                news.setPicUrl("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F012be558f09780a8012049ef824ce5.jpg%401280w_1l_2o_100sh.jpg&refer=http%3A%2F%2Fimg.zcool.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1663721105&t=a3ef42bb4519a6064902bb52e94a6437");
                // 跳转的url
                news.setUrl("");

                return messageUtil.sendImageAndText(map,news).replaceAll("com.wx.entity.News","item");
            }
            // 关注 和 取消关注类型
            case MessageUtil.REQ_MESSAGE_TYPE_EVENT : {
                if (map.get("Event").equals("subscribe")){
                    News news = new News();
                    news.setTitle("欢迎关注");
                    news.setDescription("这里是董哈哈的小镇");
                    news.setPicUrl("");
                    news.setUrl("");
                    return messageUtil.sendImageAndText(map,news);
                }else {
                    return messageUtil.sendText(map,"取消订阅");
                }
            }
            default:
                return messageUtil.sendText(map,"其他消息类型");
        }
    }

}
